import json

import requests

import loadInterData
from faker import Faker

fake = Faker('zh_CN')
dataInter = loadInterData.load_inter_df(header=["userID", "itemID", "rating"], datapath="../interData/data.csv",
                                        sep=',',
                                        has_header=True)
# # 获取数据集的行数和列数
num_rows = dataInter.shape[0]
num_cols = dataInter.shape[1]

last_index=0
interIndex=800 #交互数据到第几行了
inserLen=1000  #插入多少条
# # 打印数据集的行数和列数
print("该数据集共有{}行，{}列。".format(num_rows, num_cols))
for row_index in range(interIndex,inserLen):  # 第一个索引值，看上次你读取到哪里的索引--------------
    last_index=row_index
    row_data = dataInter.iloc[row_index]
    userID = row_data['userID']
    itemID = row_data['itemID']
    rating = row_data['rating']

    urlItemSave = 'http://127.0.0.1:8080/api/admin/store/product/save'
    urlCommentSave = 'http://127.0.0.1:8080/api/admin/store/product/reply/save'
    # headers = {'Authorization': PEXELS_API_KEY}
    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320',
        'Authori-Zation': '3a215db7a92d46aca8147eb93485151e',
        'Content-Type': 'application/json'
    }
    #       评分数据
    jsonComment = '{ "avatar": "http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/3316070ce6bb49aab05776cd21d91b21pra6fxiblz.png", "comment": "comment", "nickname": "comment", "pics": "[\\"http://127.0.0.1:8080/crmebimage/public/maintain/2021/12/25/b73c648fb7fd45cf8782e4ca0bc28a42s2a8dpad2x.png\\"]", "productId": 8, "productScore": 4, "serviceScore": 4, "sku": "默认" }'
    productComment = json.loads(jsonComment)
    productComment['productScore'] = rating
    productComment['serviceScore'] = rating
    productComment['productId'] = itemID
    productComment['comment'] = fake.paragraph()
    productComment['nickname'] = fake.word()
    productComment = json.dumps(productComment)
    # 插入item评分*******************
    response = requests.post(urlCommentSave, data=productComment, headers=headers)
    if response.status_code == 200:
        data = response.json()
        # print(data)
        if data['code'] == 200:
            print(f"插入item{userID}{itemID}{rating}评论评分成功")
        else:
            print(f"插入item{userID}{itemID}{rating}评论评分失败")
    else:
        print(f"插入item{userID}{itemID}{rating}评论评分失败")
    # 评分*******************


print('++++++++++++++++++++++++++++++++')
print(f"读取交互数据到索引为{last_index}")
re = {
    '读取交互数据到索引为': last_index,
    '插入条数':inserLen
}
# 数据库出错了，
with open('./re/insetResult_Inter' + str(interIndex) + '.json', 'w', encoding='utf-8') as f:
    json.dump(re, f, ensure_ascii=False, indent=4)
# # 获取第5行数据，行索引号为4
# row_index = 99999
# row_data = data.iloc[row_index]
# # 打印第5行数据
# print(type(row_data))
# print(row_data)
# print(row_data['userID'])

#它大爷的 后面发现其实它的电影ID就是1-1682 所以干脆直接插入电影就好了，，压根不需要读取交互文件  操他吗的、日 浪费老子电脑何时间
